<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <!-- 从相邻元素对还原数组 -->
    <script>
      function fn(pairs) {
        const ans = [],
          map = {}
        // 建图
        for (const [p, q] of pairs) {
          ;(map[p] = map[p] || []).push(q)
          ;(map[q] = map[q] || []).push(p)
        }
        // 找叶节点
        let p, q
        for (const f in map) {
          if (map[f].length === 1) {
            ans.push((p = +f))
            q = map[f][0]
            break
          }
        }
        // 遍历
        while (q !== undefined) {
          ans.push(q)
          ;[p, q] = [q, map[q][+(map[q][0] === p)]]
        }
        return ans
      }
    </script>
  </body>
</html>
